package com.jack.helloworld.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jack.helloworld.R;

public class WebViewActivity extends AppCompatActivity {

    WebView webView;
    boolean isLoadUrl = false;

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        initView();
        initData();
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.webView);
    }

    private void initData() {
        String url = "https://mp.weixin.qq.com/s?__biz=MzIwMTI4Nzk5Ng==&mid=2650220708&idx=1&sn=221e09ab9a38ba906941a5a9e8949bb0&chksm=8ef3b8f7b98431e132475e42f8bb6ff0f2fbfc85da5a1a0b4af92cb9b67b35ad9b512895bfd4&scene=0&subscene=10000&clicktime=1551249936&ascene=7&devicetype=android-28&version=2700033a&nettype=WIFI&abtest_cookie=AwABAAoACwATAAMAI5ceAFqZHgDNmR4AAAA%3D&lang=zh_CN&pass_ticket=fuXkOBIy5at7hKmkl4vdb4Iz3EzsFaMx0j5Wgs8oVeRIkDr3ajOyyc9fFi09JyLc&wx_header=1";
        //String url = "http://www.hao123.com";
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("jack", "shouldOverrideUrlLoading" + " ,url:" + url);
                view.loadUrl(url);
                return true;
            }

//            @Override
//            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                if (!isLoadUrl) {
//                    Log.d("jack", "onPageStarted");
//                    view.loadUrl(url);
//                    isLoadUrl = true;
//                }
//                super.onPageStarted(view, url, favicon);
//            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    closeDialog();
                } else {
                    updateDialog(newProgress);
                }

            }
        });

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        settings.setSupportZoom(true);//是否可以缩放，默认true
        settings.setBuiltInZoomControls(true);//是否显示缩放按钮，默认false
        settings.setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        //settings.setAppCacheEnabled(true);//是否使用缓存
        settings.setDomStorageEnabled(true);//DOM Storage
        settings.setAppCacheMaxSize(1024*1024*8);//设置缓冲大小，我设的是8M
        String appCacheDir = this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
        settings.setAppCachePath(appCacheDir);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    
    }

    private void updateDialog(int newProgress) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("loading");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMessage("loading web");
            progressDialog.setProgress(newProgress);
            progressDialog.show();
        } else {
            progressDialog.setProgress(newProgress);
        }
    }

    private void closeDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();

        super.onDestroy();
    }
}
